package service

import (
	"boy-go/modules/system/model"
	"boy-go/modules/system/repository"
	"boy-go/modules/system/vo"
	"boy-go/pkg/redis"
	"boy-go/pkg/request"
	"boy-go/pkg/xcache"
	"boy-go/pkg/xstr"
	"context"
	"github.com/jinzhu/copier"
)

func NewSysConfigService() SysConfigService {
	return &sysConfigService{
		SysConfigRepo: repository.NewSysConfigRepository(),
	}
}

type sysConfigService struct {
	SysConfigRepo *repository.SysConfigRepository
}

// GetById 根据id获取配置信息
func (s *sysConfigService) GetById(configId int64) (*vo.SysConfigModel, error) {
	m, err := s.SysConfigRepo.GetById(configId)
	if err != nil {
		return nil, err
	}
	rsp := new(vo.SysConfigModel)
	if err := copier.Copy(rsp, m); err != nil {
		return nil, err
	}
	sqlDB, err := s.SysConfigRepo.DB.DB()
	sqlDB.Stats()
	return rsp, nil
}

// List 获取参数配置表列表
func (s *sysConfigService) List(req *vo.QuerySysConfigReq) (*vo.ListSysConfigRsp, error) {

	var pageReq = &request.PageRequest{
		PageNum:  req.PageNum,
		PageSize: req.PageSize,
	}

	list, total, err := s.SysConfigRepo.List(req, pageReq)

	if err != nil {
		return nil, err
	}

	itemList := make([]*vo.SysConfigModel, 0)
	for _, v := range list {
		item := new(vo.SysConfigModel)
		if err := copier.Copy(item, &v); err != nil {
			continue
		}
		itemList = append(itemList, item)
	}
	return &vo.ListSysConfigRsp{
		List:  itemList,
		Total: total,
	}, nil
}

// Add 添加参数配置表
func (s *sysConfigService) Add(req *vo.AddSysConfigReq) (*vo.AddSysConfigRsp, error) {
	m := new(model.SysConfig)
	err := copier.Copy(m, req)
	if err != nil {
		return nil, err
	}
	if err := s.SysConfigRepo.Save(m); err != nil {
		return nil, err
	}
	return &vo.AddSysConfigRsp{
		ConfigId: m.ConfigId,
	}, nil
}

// Edit 修改参数配置表
func (s *sysConfigService) Edit(req *vo.EditSysConfigReq) (*vo.EditSysConfigRsp, error) {
	m := new(model.SysConfig)
	err := copier.Copy(m, req)
	if err != nil {
		return nil, err
	}
	if err := s.SysConfigRepo.Update(m); err != nil {
		return nil, err
	}
	return &vo.EditSysConfigRsp{
		ConfigId: m.ConfigId,
	}, nil
}

// Del 删除参数配置表
func (s *sysConfigService) Del(configId int64) error {
	return s.SysConfigRepo.Delete(configId)
}

func (s *sysConfigService) BatchDel(ids []string) error {
	return s.SysConfigRepo.DeleteBatch(ids)
}

func (s *sysConfigService) SelectConfigByKey(configKey string) *string {
	ctx := context.Background()
	configValue := redis.GetValue(ctx, s.getCacheKey(configKey))
	if configValue != nil && *configValue != "" {
		return configValue
	}
	sysConfig, err := s.SysConfigRepo.GetByKey(configKey)
	if err != nil {
		return nil
	}
	configValue = sysConfig.ConfigValue
	redis.SetValue(ctx, s.getCacheKey(configKey), *configValue)
	return configValue
}

func (s *sysConfigService) getCacheKey(configKey string) string {
	return xcache.SYS_CONFIG_KEY + configKey
}

// SelectCaptchaEnabled 获取验证码开关
func (s *sysConfigService) SelectCaptchaEnabled() bool {
	captchaEnabled := s.SelectConfigByKey("sys.account.captchaEnabled")
	if captchaEnabled == nil || *captchaEnabled == "" {
		return true
	}
	//string 转 bool
	return xstr.StringToBool(captchaEnabled)
}
